package com.origin.maplibrary

import android.Manifest
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.MarkerOptions
import com.origin.utils.permissions.OnPermissionPageCallback
import com.origin.utils.permissions.PermissionsUtils
import kotlinx.android.synthetic.main.activity_map.*


class MapActivity : AppCompatActivity(), AMapLocationListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_map)
        AMapLocationClient.updatePrivacyShow(this, true, true)
        AMapLocationClient.updatePrivacyAgree(this, true)

    }

    val mlocationClient: AMapLocationClient by lazy {
        AMapLocationClient(this);
    }


    public fun get2dMapPage(view: View) {
        startActivity(Intent(this, Map2DActivity::class.java))
    }

    public fun getLocationInfo(view: View) {
        PermissionsUtils.simpleRequestPermissions(this,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
            onPermissionPageCallback = object : OnPermissionPageCallback {
                override fun onGranted(permissions: MutableList<String>?) {

                    val mLocationOption = AMapLocationClientOption();
                    mLocationOption.locationMode =
                        AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
                    mLocationOption.interval = 2000;


                    mlocationClient.setLocationOption(mLocationOption)
                    mlocationClient.startLocation();
                    mlocationClient.setLocationListener(this@MapActivity)
                }

                override fun onDenied(permissions: MutableList<String>?) {

                }

            })

    }

    override fun onLocationChanged(amapLocation: AMapLocation?) {
        amapLocation?.apply {
            locationInfo.text = this.toString().replace("#", "\n")
        }
    }
}